package org.example.demo01.repository;

import org.example.demo01.entity.Dispatch;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface DispatchRepository extends JpaRepository<Dispatch, String> {
    // 返回列表，按qid排序
    List<Dispatch> findByWsidAndStatusOrderByQidAsc(String wsid, Integer status);

    // 新增：获取工位所有调度
    List<Dispatch> findByWsid(String wsid);

    // 新增：获取工位当前正在加工的任务
    @Query("SELECT d FROM Dispatch d WHERE d.wsid = :wsid AND d.status = 2")
    List<Dispatch> findProcessingByWsid(String wsid);

    List<Dispatch> findListByWsidAndStatus(java.lang.String wsid, int i);
    @Query(value = "INSERT INTO dispatch (pieid, wsid, qid, status) VALUES (:pieid, :wsid, :pid, :status)", nativeQuery = true)
    void saveDispatch(@Param("pieid") String pieid, @Param("wsid") String wsid, @Param("qid") String pid, @Param("status") int status);
    // 在 DispatchRepository 接口中添加
    List<Dispatch> findByWsidOrderByQidAsc(String wsid);
    List<Dispatch> findByWsidAndStatus(String wsid, Integer status);
    @Query("SELECT d FROM Dispatch d WHERE d.wsid = :wsid AND d.status IN :statusList")
    List<Dispatch> findByWsidAndStatusIn(
            @Param("wsid") String wsid,
            @Param("statusList") List<Integer> statusList
    );
}